package com.example.lixiaolong.timezone;

import android.app.AlarmManager;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;


public class MainActivity extends AppCompatActivity {
    class ZoneInfo{
        public int offset;
        public String name;

        public int hour;
        public int minute;
    }
    private class ZoneInfoComparator implements  Comparator {
        @Override
        public int compare(Object lhs, Object rhs) {
            ZoneInfo l = (ZoneInfo)lhs;
            ZoneInfo r = (ZoneInfo)rhs;
            if (l.offset>r.offset) {
                return 1;
            }
            return -1;
        }
    }

    static List<ZoneInfo> zoneInfos = new ArrayList<ZoneInfo>();
    static int zoneIndex;
    static int zoneDefaultIndex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        System.out.println("TimeZone support:");
        String[] ids = TimeZone.getAvailableIDs();
        for (int i=0; i!=ids.length; ++i) {
            int offset = TimeZone.getTimeZone(ids[i]).getRawOffset();
            if (!this.isExistZoneInfo(offset)) {
                ZoneInfo zone = new ZoneInfo();
                zone.offset = offset;
                zone.name = ids[i];
                int sign = 1;
                if (offset<0) {
                    sign = -1;
                    offset = -offset;
                }
                offset /= 1000;
                int hour = offset/3600;
                int minute = (offset-hour*3600)/60;
                zone.hour = sign*hour;
                zone.minute = minute;

                zoneInfos.add(zone);
            }
            else {
                System.out.println("抛弃:" + ids[i]);
            }
        }

        //排序
        Comparator comp = new ZoneInfoComparator();
        Collections.sort(zoneInfos,comp);

        int offset = TimeZone.getDefault().getRawOffset();
        for (int i=0; i!=zoneInfos.size(); ++i) {
            System.out.println(i + "号:" + zoneInfos.get(i).offset + " " + zoneInfos.get(i).hour + ":" + zoneInfos.get(i).minute + " " + zoneInfos.get(i).name);
            if (zoneInfos.get(i).offset==offset) {
                zoneIndex = i;
            }
            if (zoneInfos.get(i).hour==8 && zoneInfos.get(i).minute==0) {
                zoneDefaultIndex = i;
            }
        }

        this.refreshTimeZoneLabel();
    }
    private boolean isExistZoneInfo(int offset) {
        for (int i=0; i!=zoneInfos.size(); ++i) {
            if (zoneInfos.get(i).offset==offset) {
                return true;
            }
        }
        return false;
    }

    public void refreshTimeZoneLabel() {
        TimeZone zone = TimeZone.getTimeZone(zoneInfos.get(zoneIndex).name);
        String s = zone.getDisplayName(false, TimeZone.SHORT);
        System.out.println("当前时区是:" + s);

        TextView view = (TextView)findViewById(R.id.zone_time);
        view.setText(s);
    }
    public void refreshTimeZone() {
        AlarmManager timeZone = (AlarmManager) getSystemService(ALARM_SERVICE);
        timeZone.setTimeZone(zoneInfos.get(zoneIndex).name);
    }

    //////////
    public void onClick_MinTimeZone(View view) {
        System.out.println("最小时区");
        if (zoneIndex!=0) {
            zoneIndex = 0;
            this.refreshTimeZone();
            this.refreshTimeZoneLabel();
        }
    }
    public void onClick_MaxTimeZone(View view) {
        System.out.println("最大时区");
        if (zoneIndex!=zoneInfos.size()-1) {
            zoneIndex = zoneInfos.size()-1;
            this.refreshTimeZone();
            this.refreshTimeZoneLabel();
        }
    }
    public void onClick_ResetTimeZone(View view) {
        System.out.println("重置时区");
        if (zoneIndex!=zoneDefaultIndex) {
            zoneIndex = zoneDefaultIndex;
            this.refreshTimeZone();
            this.refreshTimeZoneLabel();
        }
    }
    public void onClick_DecreaseTimeZone(View view) {
        System.out.println("减小时区");
        if (zoneIndex!=0) {
            --zoneIndex;
            this.refreshTimeZone();;
            this.refreshTimeZoneLabel();
        }
    }
    public void onClick_IncreaseTimeZone(View view) {
        System.out.println("增加时区");
        if (zoneIndex!=zoneInfos.size()-1) {
            ++zoneIndex;
            this.refreshTimeZone();;
            this.refreshTimeZoneLabel();
        }
    }
}
